package cn.tedu.csmall.stock.webapi.quartz;


import cn.tedu.csmall.commons.pojo.stock.model.Stock;
import cn.tedu.csmall.stock.webapi.quartz.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

@Slf4j
public class QuartzJob implements Job {

    // 向RabbitMQ发送消息
    // 也是从Spring容器中获取SpringBoot启动时,创建好的操作RabbitMQ的对象
    @Autowired
    private RabbitTemplate rabbitTemplate;

    static int i=1;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 一个简单的任务演示
        // 输出当前系统时间,使用sout或log皆可
        log.info("---------------"+ LocalDateTime.now() +"-----------------"+i);
        // 实例化Stock对象
        Stock stock=new Stock();
        stock.setId(i++);
        stock.setCommodityCode("PC100");
        stock.setReduceCount(RandomUtils.nextInt(20)+1);
        // 利用RabbitTemplate发送消息
        //convertAndSend([交换机名称],[路由key名称],[要发送的对象])
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.STOCK_EX,
                RabbitMQConfig.STOCK_ROUT,
                stock);
        log.info("发送消息完成:{}",stock);


    }
}
